//
//  DiscoverCollectionViewCellOfContentView.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/31.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverCollectionViewCellOfContentView.h"
#import "XYMacro.h"


@interface DiscoverCollectionViewCellOfContentView () <XY_Advertisement_CollectionViewDelegate>

@property (nonatomic, strong) XY_AdvertisementView_CollectionView *tableViewHeaderScrollView;

@end

@implementation DiscoverCollectionViewCellOfContentView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _contentTableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        [self.contentView addSubview:_contentTableView];
        _contentTableView.dataSource = self;
        _contentTableView.delegate = self;
        //取出TableView多余的横线
        _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _contentTableView.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
        _contentTableView.showsVerticalScrollIndicator = NO;
        [self createTableViewHeaderView];
        [_contentTableView registerClass:[DiscoverTableViewCellOne class] forCellReuseIdentifier:@"DiscoverTableViewCellOne"];
        [_contentTableView registerClass:[DiscoverTableViewCellTwo class] forCellReuseIdentifier:@"DiscoverTableViewCellTwo"];
        [_contentTableView registerClass:[DiscoverTableViewCellThird class] forCellReuseIdentifier:@"DiscoverTableViewCellThird"];
        [_contentTableView registerClass:[DiscoverTableViewCellFourth class] forCellReuseIdentifier:@"DiscoverTableViewCellFourth"];
        [self pullDownRefresh];
        
    }
    return self;
}

#pragma mark ---------创建头视图------------
- (void)createTableViewHeaderView {
    UIView *customHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3.8)];
    _tableViewHeaderScrollView = [[XY_AdvertisementView_CollectionView alloc] initWithFrame:customHeaderView.bounds];
    [customHeaderView addSubview:_tableViewHeaderScrollView];
    _tableViewHeaderScrollView.delegate = self;
    _contentTableView.tableHeaderView = customHeaderView;
    
}

#pragma mark --- 刷新------
- (void)pullDownRefresh {
    MJRefreshGifHeader *header = [MJRefreshGifHeader  headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.contentTableView.mj_header beginRefreshing];
    
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"list_load_%d", i]];
        [mArr addObject:image];
    }
    
    [header setImages:mArr forState:MJRefreshStateRefreshing];
    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.contentTableView.mj_header = header;
}

- (void)loadNewData {
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/discovery.jhtml" andBody:@"json=%7B%7D" andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
         DiscoverModelFirst *model = [DiscoverModelFirst modelWithDictionary:result];
        if (model.status.succeed == 1) {
            _model = model;
            [self.contentTableView reloadData];
            [self.contentTableView.mj_header endRefreshing];
        }
    } andFailureBlock:^(NSError *error) {}];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.data.best_musiclist.count + 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DiscoverTableViewCellFourth *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverTableViewCellFourth" forIndexPath:indexPath];
        cell.model = _model.data;
        return cell;
        
    }
//        else if (indexPath.row == 1) {
//        DiscoverTableViewCellThird *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverTableViewCellThird" forIndexPath:indexPath];
//        cell.model = [_model.data.event_list firstObject];
//        return cell;
//        
//    }
    else if (indexPath.row == 1) {
        DiscoverTableViewCellTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverTableViewCellTwo" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
        cell.model = _model.data;
        return cell;

        
    } else {
        DiscoverTableViewCellOne *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverTableViewCellOne" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
        DiscoverModelFirstMain *model = [_model.data.best_musiclist objectAtIndex:indexPath.row - 2];
        cell.model = model;
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 1) {
        VCOfDiscoverList *nextDiscoverListVC = [[VCOfDiscoverList alloc] init];
        VCOfDiscover *discoverVC = (VCOfDiscover *)self.nextResponder.nextResponder.nextResponder;
        [discoverVC.navigationController pushViewController:nextDiscoverListVC animated:YES];
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        return 500;
    }
//    else if (indexPath.row == 1) {
//        
//        return 150;
//    }
      else if (indexPath.row == 1) {
        
        return 180;
    } else {
        
        return 170;
    }
}



- (void)didClickImageAtIndex:(NSInteger)index scrollView:(XY_AdvertisementView_CollectionView *)scrollView {
    VCOfDiscover *discoverVC =  (VCOfDiscover *)self.nextResponder.nextResponder.nextResponder;
    
    VCOfDiscoverList *nextVC = [[VCOfDiscoverList alloc] init];
    
    //传数据
    DiscoverModelFirstScroll *dataModel = [_model.data.banner_list objectAtIndex:index];
    nextVC.item_id = [NSString stringWithFormat:@"%ld", dataModel.item_id];
    [discoverVC.navigationController pushViewController:nextVC animated:YES];
    
}

#pragma mark --------重写set 赋值-----------------
- (void)setModel:(DiscoverModelFirst *)model {
    
    _model = [[DiscoverModelFirst alloc] init];
    _model = model;
    
    NSMutableArray *mArr = [NSMutableArray array];
    for (DiscoverModelFirstScroll *dataModel in model.data.banner_list) {
        [mArr addObject:dataModel.content_img];
    }
    [self.tableViewHeaderScrollView handleDataSoutrce:mArr collectionView:nil];
    [self.contentTableView reloadData];
}


@end
